fix(security): replace static CSP with per-request nonce — #311 broke hydration - #326
Merged
Conversation
… hydration PR #311 set a static `script-src 'self'` CSP assuming build-time SRI integrity hashes cover all framework scripts. SRI only covers external `<script src>` tags; the App Router RSC streaming payload (`self.__next_f.push(...)`) is delivered via inline scripts, which were all blocked — React never hydrated and every page was left as an inert SSR skeleton (6 blocked inline scripts per page on dev). Move CSP delivery from static next.config headers() into src/proxy.ts: - generate a cryptographic random nonce per request - script-src 'self' 'nonce-<random>' 'strict-dynamic' (no unsafe-inline) - mirror the nonce into request headers so Next.js applies it to framework scripts (verified: inline __next_f scripts carry the nonce) - keep SRI enabled for external scripts The dynamic-rendering cost that motivated SRI over nonce in #311 does not apply here: every page is already server-rendered behind the locale proxy (only /_not-found is static). Verified locally with a production build: zero CSP console errors, login form and market order book hydrate correctly. Adds tests/unit/proxy-csp.test.ts (5 cases).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
#311 set a static
script-src 'self'CSP assuming build-time SRI integrity hashes cover all framework scripts. SRI only covers external<script src>tags — the App Router RSC streaming payload (self.__next_f.push(...)) is delivered via inline scripts, which were all blocked. React never hydrated: every page was an inert SSR skeleton (6 blocked inline scripts per page observed on dev, e.g. the home page rendered only the static 'Login / Sign Up' shell).Surfaced now because dev last deployed 2026-07-09 (
next-92db9dd), predating #311. Any production deploy of #311+ would break the same way.Fix
Move CSP delivery from static
next.config.ts headers()intosrc/proxy.ts:crypto.getRandomValues, 16 bytes, base64)script-src 'self' 'nonce-<random>' 'strict-dynamic'— nounsafe-inline;'strict-dynamic'covers webpack runtime chunk loading__next_fscripts carry the nonce attribute)/_not-foundis static)Test plan
pnpm verify(type-check + lint + coverage + build) passestests/unit/proxy-csp.test.ts: 5 cases (nonce present / no unsafe-inline in script-src / fresh nonce per request / request-header nonce consistency / healthcheck short-circuit unaffected)